<template>
	<view style="background-color: #fff;">
		<view class="repair">
			<view class="uni-title" style="width: 30%;">请假类型：</view>
			<view class="select-list">
				<picker @change="bindPickerChange" :value="index" :range="array" range-key="name">
					<view>{{array[index].name}}</view>
				</picker>
			</view>
		</view>
		<view class="repair">
			<view class="uni-title" style="width: 30%;">开始时间：</view>
			<view class="select-list">
				<h-timePicker @changeTime="changeStartTime">
					<view slot="pCon" class="changeTime">
					    {{start_time}}
					  </view>
				</h-timePicker>
			</view>
		</view>
		<view class="repair">
			<view class="uni-title" style="width: 30%;">结束时间：</view>
			<view class="select-list">
				<h-timePicker @changeTime="changeEndTime">
					<view slot="pCon" class="changeTime">
					    {{end_time}}
					  </view>
				</h-timePicker>
			</view>
		</view>
		
		<view class="repair">
			<view class="uni-title" style="width: 30%;">请假天数：</view>
				<textarea style="height:50upx;" v-model="day" />
		</view>
		<view class="repair">
			<view class="uni-title" style="width: 25%;">审批人：</view>
				<view class="uni-list-cell-db">
					<picker-c @change="handleApprover" :aList="aList"></picker-c>
				</view>
		</view>
		<view class="repair">
			<view class="uni-title">请输入请假内容：</view>
		</view>
		<view class="repair-text">
			<textarea style="height:300upx;" v-model="content" placeholder="请输入内容"/>
		</view>
		<view class="repair-botton">
			<button type="primary" :loading="loading" :disabled="disabled" @tap="handleClick">提交</button>
		</view>
		<neil-modal :show="show" align="center" @close="closeModal()" :show-cancel="false" title="操作提示" content="发起流程">
		</neil-modal>
	</view>
</template>
<script>
	import hTimePicker from "@/components/h-timePicker/h-timePicker.vue"
	import {apiLeave,apigetProcessList} from '@/common/api.js';
	import neilModal from '@/components/neil-modal/neil-modal.vue';
	export default {
		name:"repair",
		components:{
			neilModal,
			hTimePicker
		},
		data() {
			const currentDate = this.getDate();
			return {
				loading:false,
				disabled:false,
				content:'',
				index: 0,
				show:false,
				start_time:currentDate[0],
				end_time:currentDate[1],
				day:'1',
				array: [{name:'事假'},{name: '病假'}, {name:'婚假'}, {name:'产假'}, {name:'丧假'}, {name:'年假'}],
				user:'', 
				role_name:uni.getStorageSync('user_info').role_name,
				aList:[]
			}
		},
		mounted(){
			let data={
				taskid: "", //(必填)流程任务id
			    processInstanceId: "", //(必填)流程实例id
			    key: "Leave_flow", //(必填)流程定义key
			    position: this.role_name, //(必填)申请人角色
			    type: "new" //(必填)新增new/运行中
				}
			apigetProcessList(data).then(res=>{
				console.log(res)
				let userTaskName = res.data.userlist.userTaskName;
				this.aList=res.data.userlist.userList
				this.user=userTaskName === "结束"?0:this.aList&&this.aList!=""?this.aList[0]:""
				console.log(this.user)
			})
		},
		methods: {
			// 选择结束时间
			changeEndTime(time){
				this.end_time=time
			},
			// 选择开始时间
			changeStartTime(time){
				this.start_time=time
			},
			// 选择请假类型
			bindPickerChange(e) {
				this.index = e.target.value
			},
			// 选择审核人
			handleApprover(e){
				this.user = e 
			},
			getDate() {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				
				let hour=date.getHours()
				let min=date.getMinutes()
				let second=date.getSeconds()
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				hour=hour>9?hour:'0'+hour
				min=min>9?min:'0'+min
				second=second>9?second:'0'+second
				return [`${year}-${month}-${day} ${hour}:${min}:${second}`,`${year}-${month}-${day} 18:00:00`];
			},
			handleClick(){
				if(!this.content){
					 this.toast('请假内容不能为空') 
					 return
				}
				if(this.start_time>=this.end_time){
					this.toast('开始时间不能晚于结束时间')
					return
				}
				this.loading = true
				const data = {
					leave_category:this.index,//(必填)请假类别；
					start_time:this.start_time,//(必填)起始时间；
					end_time:this.end_time,//(必填)结束时间；
					reason:this.content,//(必填)事由；
					day_count:this.day,//(必填)请假天数；
					userid:this.user.userid
				}
				console.log(JSON.stringify(data))
				uni.showModal({
					content:'你选择的审批人是'+this.user.username + ',确定提交吗？'
				}).then(res=>{
					if(res[1].confirm){
						apiLeave(data).then(res=>{
							this.toast('申请成功！')
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							},1200)
						}).catch(err=>{
							this.loading = false
							this.toast('当前无此权限，请联系管理员')
						})
					}else{
						this.loading = false
					}
					
				})
			},
			closeModal() {
			    this.show = false
				uni.navigateBack()
			},
			toast(title,icon='none'){
				uni.showToast({
					title: title,
					mask: true,
					icon:icon,
					duration: 1500
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.repair{
		display: flex;
		align-items:center;
		padding: 20upx 30upx;
		box-sizing:border-box;
		border-bottom: 1px solid #eee;
		&-radios{
			padding-left: 30upx;
		}
	}
	.repair-text{
		padding: 30upx;
	}
	.repair-botton{
		padding: 30upx;
	}
	.select-list{
		flex: 2;
	}
</style>
